import React, {useEffect, useState} from 'react'
import type {ColumnsType} from 'antd/es/table'
import {Button, Select} from 'antd'
import {history} from 'umi'
import CustomTable from '@/components/Table/index'
import SearchForm from '@/components/SearchForm'
import {getPublicDownloadUrl} from '@/service/common/upload'

interface DataType {
	tenderSubmitTime: string
	imgHref: string | undefined
	bidId: string | string[] | null
	id: string
	supplierCode: string
	supplierName: string
	serviceType: string
	contacts: string
	status: string
	date: string
}
const formItemLayout = {
	labelCol: {flex: '70px'},
	wrapperCol: {flex: '1'},
}

const bidding = (props: any) => {
	const {data, dictData} = props
	const [searchData, setSearchData] = useState<any>()
	const [total, setTotal] = useState<any>()
	const [dataSource, setDataSource] = useState<any[]>([])
	const [allDataSource, setAllDataSource] = useState<any[]>([])
	const [params, setParams] = useState({
		current: 1,
		size: 10,
	})
	useEffect(() => {
		const newBiddingAnswerList = data?.biddingAnswerList || []
		const urls = newBiddingAnswerList?.map(
			(item: {biddingFileUrl: string}) => Number(item?.biddingFileUrl),
		)
		if (urls?.length) {
			getPublicDownloadUrl(urls).then((result: any) => {
				if (result?.success) {
					result?.data?.forEach(
						(item: {
							targetAttr: number
							id: any
							filename: any
							url: any
						}) => {
							data.biddingAnswerList?.forEach(
								(
									biddingItem: {
										biddingFileUrl: string
									},
									index: number,
								) => {
									if (
										item?.id ===
										Number(biddingItem?.biddingFileUrl)
									) {
										newBiddingAnswerList[index].imgHref =
											item?.url
									}
								},
							)
						},
					)
					setAllDataSource(newBiddingAnswerList)
					setDataSource(newBiddingAnswerList)
				}
			})
		}
	}, [data])

	useEffect(() => {
		if (searchData?.projectName) {
			const newData = allDataSource?.filter(
				(item: {projectName: string}) =>
					item?.projectName.indexOf(searchData?.projectName) !== -1,
			)
			setDataSource(newData)
			setParams({
				current: 1,
				size: 10,
			})
			setTotal(newData?.length)
		} else {
			setDataSource(allDataSource)
			setTotal(allDataSource?.length)
			setParams({
				current: 1,
				size: 10,
			})
		}
	}, [searchData, allDataSource])
	const columns: ColumnsType<DataType> = [
		{
			title: '招标项目名称',
			dataIndex: 'projectName',
			align: 'center',
			width: 150,
			render: (text, record) => (
				<Button
					type="link"
					onClick={() => {
						history.push({
							pathname: `/supplier/bidding/view`,
							query: {
								id: record?.bidId,
							},
						})
					}}>
					{text}
				</Button>
			),
		},
		{
			title: '项目预算',
			dataIndex: 'projectBudget',
			align: 'center',
			width: 120,
		},
		{
			title: '标段',
			dataIndex: 'projectBidSection',
			align: 'center',
			width: 100,
			render: (text) => (
				<>
					{text ? (
						<Select
							disabled
							value={text}
							fieldNames={{value: 'dictKey', label: 'dictValue'}}
							options={dictData.Project_Bid_Section}
						/>
					) : (
						'-'
					)}
				</>
			),
		},
		{
			title: '建档时间',
			dataIndex: 'createArchivesTime',
			align: 'center',
			width: 160,
		},
		{
			title: '项目状态',
			dataIndex: 'projectStatus',
			align: 'center',
			width: 120,
			render: (text) => (
				<Select
					disabled
					value={text}
					fieldNames={{value: 'dictKey', label: 'dictValue'}}
					options={dictData.Project_Status}
				/>
			),
		},
		{
			title: '投标联系人',
			dataIndex: 'bidderPerson',
			align: 'center',
			width: 120,
		},
		{
			title: '联系电话',
			dataIndex: 'bidderPhone',
			align: 'center',
			width: 120,
		},
		{
			title: '投标状态',
			dataIndex: 'bidStatus',
			align: 'center',
			width: 120,
			render: () => '已投标',
		},
		{
			title: '投标标段',
			dataIndex: 'bidSection',
			align: 'center',
			width: 120,
			render: (text) => (
				<Select
					value={text}
					disabled
					fieldNames={{value: 'dictKey', label: 'dictValue'}}
					options={dictData?.Project_Bid_Section}
				/>
			),
		},
		{
			title: '标书递交时间',
			dataIndex: 'tenderSubmitTime',
			align: 'center',
			width: 160,
		},
		{
			title: '投标文件',
			dataIndex: 'biddingFileName',
			align: 'center',
			width: 150,
			render: (text, record) => (
				<a href={record?.imgHref} download>
					{text}
				</a>
			),
		},
	]
	const formItems: any[] = [
		{
			type: 'group',
			key: 'group1',
			groupItems: [
				{
					type: 'input',
					label: '项目名称',
					name: 'projectName',
					key: 'projectName',
				},
				{},
				{},
			],
		},
	]

	const onFinish = (value: any) => {
		setSearchData(value)
		setParams({
			current: 1,
			size: 10,
		})
	}
	const onReset = () => {
		setSearchData([])
		setParams({
			current: 1,
			size: 10,
		})
	}
	return (
		<div>
			<SearchForm
				onFinish={onFinish}
				onReset={onReset}
				formItems={formItems}
				formLayout={formItemLayout}
				style={{marginBottom: '12px'}}
			/>
			<CustomTable
				params={params}
				columns={columns}
				total={total}
				pagination
				callback={(pagination: any) => {
					setParams(pagination)
				}}
				loading={false}
				dataSource={dataSource}
			/>
		</div>
	)
}

export default bidding
